<?php
    class Admin_news extends Admin_Controller{
        function __construct(){
            parent::__construct();
            $this->load->model('News_m');
            $this->_data['sidebar']="news";
        }
         function index(){
           $this->list_news();
        }
       function list_news(){
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['full_tag_open'] = "<div id='wrapperpage'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="pageactive"> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = 'Suiv.';
        $config['prev_link'] = 'Prec.';
        $config['base_url'] = base_url() . 'index.php/admin_news/list_news/'. $per_page . '/';
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->News_m->count_news();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->News_m->get_news($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste actualités";
        $this->_data['perpage'] = $per_page;
        $this->_data['offset'] = $off_set;
        $this->display_admin('admin/news/list_news');
       }

        function add_news(){

        $config = array(
            array(
                'field' => 'title',
                'label' => 'Titre',
                'rules' => 'required'
            ),
            array(
                'field' => 'content',
                'label' => 'Contenu',
                'rules' => 'required'
            ),
             array(
                'field' => 'meta_title',
                'label' => 'Meta title',
                'rules' => 'xss_clean'
            ),
            array(
                'field' => 'meta_description',
                'label' => 'Meta description',
                'rules' => 'xss_clean'
            ),
            array(
                'field' => 'catid',
                'label' => 'Categorie',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
         if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
             $image_path = './assets/photo_news';
                $thumb_path = $image_path . '/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000,
                    'encrypt_name'=> true
                );
                $this->upload->initialize($config);
                if (!$this->upload->do_upload()) {
                      $data = array(
                        'title' => $this->input->post('title'),
                        'catid' => $this->input->post('catid'),
                        'content' => $this->input->post('content'),
                        'meta_title' => $this->input->post('meta_title'),
                        'meta_description' => $this->input->post('meta_description'),
                        'posted' =>time()
                    );
           $query=$this->News_m->add_news($data);
           if($query) $this->session->set_flashdata('error', "L'article a été ajouté !");
            else  $this->session->set_flashdata('error', "L'article n'est pas ajouté, veuillez reessayer !");
                   redirect('admin_news/list_news');

                } else {

                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'title' => $this->input->post('title'),
                       'catid' => $this->input->post('catid'),
                       'img' => $upload_info['file_name'],
                        'content' => $this->input->post('content'),
                         'meta_title' => $this->input->post('meta_title'),
                         'meta_description' => $this->input->post('meta_description'),
                        'posted' =>time()
                    );
                   $query=$this->News_m->add_news($data);
           if($query) $this->session->set_flashdata('error', "L'article a été ajouté !");
            else  $this->session->set_flashdata('error', "L'article n'est pas ajouté, veuillez reessayer !");
           redirect('admin_news/list_news');
            }}}
       $this->_data['categorie']  = $this->News_m->get_category();

        $this->display_admin('admin/news/add_news');

       
        }


         function update_news($id,$perpage,$offset){

        $config = array(
            array(
                'field' => 'title',
                'label' => 'Titre',
                'rules' => 'required'
            ),
            array(
                'field' => 'content',
                'label' => 'Contenu',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
         if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
             $image_path = './assets/photo_news';
                $thumb_path = $image_path . '/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000,
                    'encrypt_name'=> true
                );
                $this->upload->initialize($config);
                if (!$this->upload->do_upload()) {
                      $data = array(
                        'title' => $this->input->post('title'),
                        'catid' => $this->input->post('catid'),
                        'content' => $this->input->post('content'),
                        'meta_title' => $this->input->post('meta_title'),
                        'meta_description' => $this->input->post('meta_description')

                    );
           $query=$this->News_m->update_news($data, $id);
           if($query) $this->session->set_flashdata('error', "L'article a été modifié !");
            else  $this->session->set_flashdata('error', "L'article n'est pas modifié, veuillez reessayer !");
                   redirect('admin_news/list_news/'.$perpage.'/'.$offset);

                } else {
                    $query = $this->News_m->get_detail_news($id);
                    if (!empty($query)) {
                        $info = $query;
                        $image_to_delete = './assets/photo_news/' . $info['img'];
                        $thumb_to_delete = './assets/photo_news/thumbs/' . $info['img'];
                        delete_images($image_to_delete, $thumb_to_delete);
                    }
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'title' => $this->input->post('title'),
                       'catid' => $this->input->post('catid'),
                       'img' => $upload_info['file_name'],
                       'content' => $this->input->post('content'),
                         'meta_title' => $this->input->post('meta_title'),
                         'meta_description' => $this->input->post('meta_description')
                    );
                   $query=$this->News_m->update_news($data, $id);
           if($query) $this->session->set_flashdata('error', "L'article a été modifié !");
            else  $this->session->set_flashdata('error', "L'article n'est pas modifié, veuillez reessayer !");
           redirect('admin_news/list_news/'.$perpage.'/'.$offset);
            }}}
       $this->_data['categorie']  = $this->News_m->get_category();
       $this->_data['query']  = $this->News_m->get_detail_news($id);
        $this->_data['perpage'] = $perpage;
        $this->_data['offset'] = $offset;

        $this->display_admin('admin/news/update_news');

        }


        function delete_news($id,$perpage,$offset){
        $news=$this->News_m->get_detail_news($id);
        if(!empty($news)){
           $image_to_delete = './assets/photo_news/' . $news['img'];
           $thumb_to_delete = './assets/photo_news/thumbs/' . $news['img'];
            delete_images($image_to_delete, $thumb_to_delete);
        }
            $query = $this->News_m->delete_news($id);
        if ($query) $this->session->set_flashdata('error', "L'article a été supprimé !");
        else $this->session->set_flashdata('error', "L'article n'a pas été supprimé !");
        redirect('admin_news/list_news/'.$perpage.'/'.$offset);
       }
       function delete_multi_news($perpage,$offset){
         $list="";
         $adelete=$this->input->post('delete');
         $N = count($adelete);

         for($i=0; $i < $N; $i++) { 
             if ($adelete[$i]!=""){
         $list=$list.','.$adelete[$i];
         $news=$this->News_m->get_detail_news($adelete[$i]);
         
        if(!empty($news)){
           $image_to_delete = './assets/photo_news/' . $news['img'];
           $thumb_to_delete = './assets/photo_news/thumbs/' . $news['img'];
            delete_images($image_to_delete, $thumb_to_delete);
        }
         
         }
             }
         $list = '('.substr($list, 1).')';

        $query = $this->News_m->delete_multi_news($list);
        if ($query) $this->session->set_flashdata('error', "L'article a été supprimé !");
        else $this->session->set_flashdata('error', "L'article n'a pas été supprimé !");
         redirect('admin_news/list_news/'.$perpage.'/'.$offset);
       }

        function list_cat(){
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['full_tag_open'] = "<div id='wrapperpage'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="pageactive"> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = 'Suiv.';
        $config['prev_link'] = 'Prec.';
        $config['base_url'] = base_url() . 'index.php/admin_news/list_cat/'. $per_page . '/';
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->db->count_all('cat_news');
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }

        $this->_data['query'] = $this->News_m->get_cat_limit($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste de page";
        $this->_data['perpage'] = $per_page;
        $this->_data['offset'] = $off_set;
        $this->display_admin('admin/news/list_cat');
       }

        function add_cat(){

        $config = array(
            array(
                'field' => 'title',
                'label' => 'Page',
                'rules' => 'required'
            ),
             array(
                'field' => 'meta_title',
                'label' => 'Meta title',
                'rules' => 'xss_clean'
            ),
            array(
                'field' => 'meta_description',
                'label' => 'Meta description',
                'rules' => 'xss_clean'
            )
        );
        $this->form_validation->set_rules($config);
         if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
             $data = array(
                        'title' => $this->input->post('title'),
                        'meta_title' => $this->input->post('meta_title'),
                        'meta_description' => $this->input->post('meta_description')
                    );
           $query=$this->News_m->add_cat($data);
           if($query) $this->session->set_flashdata('error', "L'article a été ajouté !");
            else  $this->session->set_flashdata('error', "L'article n'est pas ajouté, veuillez reessayer !");
           redirect('admin_news/list_cat');
            }}
        $this->display_admin('admin/news/add_cat');
       }

         function update_cat($id,$perpage,$offset){

        $config = array(
            array(
                'field' => 'title',
                'label' => 'Page',
                'rules' => 'required'
            ),
             array(
                'field' => 'meta_title',
                'label' => 'Meta title',
                'rules' => 'xss_clean'
            ),
            array(
                'field' => 'meta_description',
                'label' => 'Meta description',
                'rules' => 'xss_clean'
            )
        );
        $this->form_validation->set_rules($config);
         if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
             $data = array(
                        'title' => $this->input->post('title'),
                        'meta_title' => $this->input->post('meta_title'),
                        'meta_description' => $this->input->post('meta_description')
                    );
           $query=$this->News_m->update_cat($data,$id);
           if($query) $this->session->set_flashdata('error', "L'article a été modifié!");
            else  $this->session->set_flashdata('error', "L'article n'est pas modifié, veuillez reessayer !");
           redirect('admin_news/list_cat');
            }}
        $this->_data['query']  = $this->News_m->get_detail_cat($id);
        $this->_data['perpage'] = $perpage;
        $this->_data['offset'] = $offset;
        $this->display_admin('admin/news/update_cat');
       }



        function delete_cat($id,$perpage,$offset){
        $query = $this->News_m->delete_cat($id);
        if ($query) $this->session->set_flashdata('error', "L'article a été supprimé !");
        else $this->session->set_flashdata('error', "L'article n'a pas été supprimé !");
        redirect('admin_news/list_cat/'.$perpage.'/'.$offset);
       }
       function delete_multi_cat($perpage,$offset){
         $list="";
         $adelete=$this->input->post('delete');
         $N = count($adelete);
         for($i=0; $i < $N; $i++)
         { if ($adelete[$i]!="")$list=$list.','.$adelete[$i];}
         $list = '('.substr($list, 1).')';

        $query = $this->News_m->delete_multi_cat($list);
        if ($query) $this->session->set_flashdata('error', "L'article a été supprimé !");
        else $this->session->set_flashdata('error', "L'article n'a pas été supprimé !");
        redirect('admin_news/list_cat/'.$perpage.'/'.$offset);
       }
    }
?>
